var http = require("http");
var url = require("url");
var util = require('util');

function start(route) {
    function get1(request, response) {
        var parseUrl = url.parse(request.url, true);
        route(parseUrl.pathname);
        response.writeHead(200, {"Content-Type": "text/plain"});
        response.write("Hello World");
        response.end();
    }

    function get2(req, res) {
        var parseUrl = url.parse(req.url, true);
        route(parseUrl.pathname);
        var params = parseUrl.query;
        for(var o in params){
            console.log("key = "+ o.toString() +"  value = "+ params[o]);
        }
        res.writeHead(200, {'Content-Type': 'text/plain; charset=utf-8'});
        res.end(util.inspect(url.parse(req.url, true)));
    }

    function post1(req, res) {
        var parseUrl = url.parse(req.url, true);
        route(parseUrl.pathname);
        var data = "";
        req.on("data",function (chunk) {
            data+=chunk;
        });
        req.on("end",function () {
            res.writeHead(200, {'Content-Type': 'text/plain; charset=utf-8'});
            res.end(util.inspect(url.parse(req.url, true)));
        });
    }

    http.createServer(get1).listen(8888);
    http.createServer(get2).listen(8889);
    http.createServer(post1).listen(8880);
    console.log("Server has started.");
}

exports.start = start;
